<?php

namespace app\admin\model;
use app\common\library\helper;
use app\common\model\Store as StoreModel;
use app\admin\service\Comment;
use app\admin\service\Goods;
use app\admin\service\Order;
use app\admin\service\User;
use think\Session;
use think\Db;
/**
 * 商城模型
 */
class Store extends StoreModel{
    /* @var Goods $GoodsModel */
    private $GoodsModel;
    /* @var Order $GoodsModel */
    private $OrderModel;
    /* @var User $GoodsModel */
    private $UserModel;
    /**
     * 构造方法
     */
    public function initialize(){
        parent::initialize();
        /* 初始化模型 */
        $this->GoodsModel = new Goods;
        $this->OrderModel = new Order;
        $this->UserModel = new User;
    }

    /**
     * 后台首页数据
     */
    public function getHomeData(){
        // 最近七天日期
        $lately7days = $this->getLately7days();
        // 获取当前登录人所属权限的数据
        $sessionInfo=Session::get('yoshop_adminuser');
        if($sessionInfo['user']['type']==1){
            $info['widget-card']=[
                //自营公司总量
                'service_oem'=>$this->getServiceCount(1),
                //渠道公司总量
                'service_isp'=>$this->getServiceCount(2),
                //客户总量
                'service_user'=>$this->getKehu('all',$sessionInfo),
            ];
        }elseif ($sessionInfo['user']['type']==2){
            //自营
            $info['widget-card']=[
                //渠道公司总量
                'service_isp'=>$this->getServiceCount2($sessionInfo),
                //客户总量
                'service_user'=>$this->getKehu('zy',$sessionInfo),
            ];
        }else{
            $info['widget-card']=[
                //客户总量
                'service_user'=>$this->getKehu('qd',$sessionInfo),
            ];
        }
        $data = [
            'widget-card' => $info['widget-card'],//客户总量
            'widget-echarts' => [
                'companyName'=>Db::name('yonghui_company')->where(['id'=>$sessionInfo['user']['company_id']])->value('name')?Db::name('yonghui_company')->where(['id'=>$sessionInfo['user']['company_id']])->value('name'):'永辉联邦',
                // 最近七天日期
                'date' => helper::jsonEncode($lately7days),
                'order_total' => helper::jsonEncode($this->getOrderTotalByDate($lately7days)),

            ]
        ];
        return $data;
    }

    //平台收入
    private function getSystemIncome($date){
        return db('divide_system')->where('status',1)->whereTime('create_time', $date)->sum('amount');
    }

    /**
     * 最近七天日期
     */
    private function getLately7days()
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }

    /**
     * 获取公司数据
     */
    private function getServiceCount($type){
        //获取公司数据
        return db('yonghui_company')->where('status',1)->where('type',$type)->count();
    }
    /**
     * 自营获取公司数据
     */
    private function getServiceCount2($sessionInfo){
        //获取公司数据
        return db('yonghui_company')->where('status',1)->where('upId',$sessionInfo['user']['company_id'])->count();
    }

    /**
     *  获取客户数据
     */
    private function getKehu($flag,$sessionInfo){
        if($flag=='all'){
            $where='1=1';
        }elseif($flag=='zy'){
            //获取当前自营公司底下所有渠道公司
            $ids=db('yonghui_company')->where(['upId'=>$sessionInfo['user']['company_id']])->select();
            $w=[];
            if(!empty($ids)){
                foreach($ids as $k=>$v){
                    $w[]=$v['id'];
                }
            }
            $w[]=$sessionInfo['user']['company_id'];
            $where['sourceId']=['in',$w];
        }else{
            $where['sourceId']=['=',$sessionInfo['user']['company_id']];
        }
        return db('yonghui_customer')->where($where)->count();
    }

    /**
     * 获取客户总量
     */
    private function getOrderTotal($day = null)
    {
        return number_format($this->OrderModel->getPayOrderTotal($day,$day));
    }

    /**
     * 获取客户总量 (指定日期)
     */
    private function getOrderTotalByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal($day);
        }
        return $data;
    }





}